﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class ConnectDuctManage
	{
		public void SelectButton(Button button)
		{
			button.PerformClick();
			button.Select();
			button.Focus();
		}

		public void ChangeButtnBackColor(GroupBox gBox, Button button)
		{
			System.Drawing.Color backColor = gBox.BackColor;
			foreach (object obj in gBox.Controls)
			{
				Button button2 = obj as Button;
				if (button2 != null)
				{
					button2.BackColor = backColor;
					if (button2 == button)
					{
						button2.BackColor = System.Drawing.Color.LightSkyBlue;
					}
				}
			}
		}

		public ConnectDuctStyle GetConnectStyle(Button button)
		{
			return (ConnectDuctStyle)Enum.Parse(typeof(ConnectDuctStyle), button.Tag.ToString());
		}

		public Button GetButtonByConnStyle(ConnectDuctStyle connStyle, GroupBox gBox)
		{
			Button result = null;
			foreach (object obj in gBox.Controls)
			{
				Button button = obj as Button;
				if (button != null && button.Tag.ToString() == connStyle.ToString())
				{
					result = button;
					break;
				}
			}
			return result;
		}

		public List<string> GetFittingNames(ConnectDuctStyle connStyle)
		{
			List<string> list = new List<string>();
			switch (connStyle)
			{
			case ConnectDuctStyle.TShapeTee:
				list.Add("矩形T型三通-顶对齐");
				list.Add("矩形T型三通-中心对齐");
				list.Add("矩形T型三通-底对齐");
				break;
			case ConnectDuctStyle.RoundAngleTee:
				list.Add("矩形圆角三通-顶对齐");
				list.Add("矩形圆角三通-中心对齐");
				list.Add("矩形圆角三通-底对齐");
				break;
			case ConnectDuctStyle.RightAngleTee:
				list.Add("矩形直角三通-顶对齐");
				list.Add("矩形直角三通-中心对齐");
				list.Add("矩形直角三通-底对齐");
				break;
			case ConnectDuctStyle.IntubationTee:
				list.Add("矩形插管三通-顶对齐");
				list.Add("矩形插管三通-中心对齐");
				list.Add("矩形插管三通-底对齐");
				break;
			case ConnectDuctStyle.YShapeTee:
				list.Add("矩形Y型三通-顶对齐");
				list.Add("矩形Y型三通-中心对齐");
				list.Add("矩形Y型三通-底对齐");
				break;
			case ConnectDuctStyle.CommonTee:
				list.Add("矩形普通三通-顶对齐");
				list.Add("矩形普通三通-中心对齐");
				list.Add("矩形普通三通-底对齐");
				break;
			case ConnectDuctStyle.DiversionElbow:
				list.Add("矩形直角弯头");
				break;
			case ConnectDuctStyle.SilencingElbow:
				list.Add("内弧形矩形弯头");
				break;
			case ConnectDuctStyle.ArcElbow:
				list.Add("内外弧形矩形弯头");
				break;
			case ConnectDuctStyle.RoundAngleCross:
				list.Add("矩形圆角四通-顶对齐");
				list.Add("矩形圆角四通-中心对齐");
				list.Add("矩形圆角四通-底对齐");
				break;
			case ConnectDuctStyle.RightAngleCross:
				list.Add("矩形直角四通-顶对齐");
				list.Add("矩形直角四通-中心对齐");
				list.Add("矩形直角四通-底对齐");
				break;
			case ConnectDuctStyle.IntubationCross:
				list.Add("矩形插管四通-顶对齐");
				list.Add("矩形插管四通-中心对齐");
				list.Add("矩形插管四通-底对齐");
				break;
			case ConnectDuctStyle.Side:
				list.Add("矩形弧线侧连接");
				break;
			case ConnectDuctStyle.Variable:
				list.Add("矩形变径");
				break;
			case ConnectDuctStyle.BackForth:
				list.Add("内外弧形矩形弯头");
				break;
			case ConnectDuctStyle.CircleSquare:
				list.Add("天圆地方");
				break;
			case ConnectDuctStyle.RightAngleTeeRound:
				list.Add("圆形直角三通-顶对齐");
				list.Add("圆形直角三通-中心对齐");
				list.Add("圆形直角三通-底对齐");
				break;
			case ConnectDuctStyle.SlantingTeeRound:
				list.Add("圆形斜角三通-顶对齐");
				list.Add("圆形斜角三通-中心对齐");
				list.Add("圆形斜角三通-底对齐");
				break;
			case ConnectDuctStyle.CommonTeeRound:
				list.Add("圆形Y型三通-顶对齐");
				list.Add("圆形Y型三通-中心对齐");
				list.Add("圆形Y型三通-底对齐");
				break;
			case ConnectDuctStyle.ArcElbowRound:
				list.Add("圆形弯头");
				break;
			case ConnectDuctStyle.ShrimpElbowRound:
				list.Add("虾米节弯头");
				break;
			case ConnectDuctStyle.RoundAngleCorssRound:
				list.Add("圆形直角四通-顶对齐");
				list.Add("圆形直角四通-中心对齐");
				list.Add("圆形直角四通-底对齐");
				break;
			case ConnectDuctStyle.IntubationCrossRound:
				list.Add("圆形插管四通-顶对齐");
				list.Add("圆形插管四通-中心对齐");
				list.Add("圆形插管四通-底对齐");
				break;
			case ConnectDuctStyle.SlantingCrossRound:
				list.Add("圆形斜四通-顶对齐");
				list.Add("圆形斜四通-中心对齐");
				list.Add("圆形斜四通-底对齐");
				break;
			case ConnectDuctStyle.SideRound:
				list.Add("圆形斜线侧连接");
				break;
			case ConnectDuctStyle.VariableRound:
				list.Add("圆形变径");
				break;
			case ConnectDuctStyle.BackForthRound:
				list.Add("圆形弯头");
				break;
			}
			return list;
		}

		public List<string> GetRoundAngleTeeNames()
		{
			return new List<string>
			{
				"矩形圆角变径三通_水平左_竖直顶对齐_法兰",
				"矩形圆角变径三通_水平右_竖直顶对齐_法兰",
				"矩形圆角变径三通_水平中_竖直顶对齐_法兰",
				"矩形圆角变径三通_水平左_竖直底对齐_法兰",
				"矩形圆角变径三通_水平右_竖直底对齐_法兰",
				"矩形圆角变径三通_水平中_竖直底对齐_法兰",
				"矩形圆角变径三通_水平左_竖直中对齐_法兰",
				"矩形圆角变径三通_水平右_竖直中对齐_法兰",
				"矩形圆角变径三通_水平中_竖直中对齐_法兰"
			};
		}

		public List<string> GetRightAngleTeeNames()
		{
			return new List<string>
			{
				"矩形直角变径三通_水平左_竖直顶对齐_法兰",
				"矩形直角变径三通_水平右_竖直顶对齐_法兰",
				"矩形直角变径三通_水平中_竖直顶对齐_法兰",
				"矩形直角变径三通_水平左_竖直底对齐_法兰",
				"矩形直角变径三通_水平右_竖直底对齐_法兰",
				"矩形直角变径三通_水平中_竖直底对齐_法兰",
				"矩形直角变径三通_水平左_竖直中对齐_法兰",
				"矩形直角变径三通_水平右_竖直中对齐_法兰",
				"矩形直角变径三通_水平中_竖直中对齐_法兰"
			};
		}

		public List<string> GetIntubationTeeNames()
		{
			return new List<string>
			{
				"矩形插管变径三通_水平左_竖直顶对齐_法兰",
				"矩形插管变径三通_水平右_竖直顶对齐_法兰",
				"矩形插管变径三通_水平中_竖直顶对齐_法兰",
				"矩形插管变径三通_水平左_竖直底对齐_法兰",
				"矩形插管变径三通_水平右_竖直底对齐_法兰",
				"矩形插管变径三通_水平中_竖直底对齐_法兰",
				"矩形插管变径三通_水平左_竖直中对齐_法兰",
				"矩形插管变径三通_水平右_竖直中对齐_法兰",
				"矩形插管变径三通_水平中_竖直中对齐_法兰"
			};
		}

		public List<string> GetYShapeTeeNames()
		{
			return new List<string>
			{
				"矩形Y型三通_水平左_竖直顶对齐_法兰",
				"矩形Y型三通_水平右_竖直顶对齐_法兰",
				"矩形Y型三通_水平中_竖直顶对齐_法兰",
				"矩形Y型三通_水平左_竖直底对齐_法兰",
				"矩形Y型三通_水平右_竖直底对齐_法兰",
				"矩形Y型三通_水平中_竖直底对齐_法兰",
				"矩形Y型三通_水平左_竖直中对齐_法兰",
				"矩形Y型三通_水平右_竖直中对齐_法兰",
				"矩形Y型三通_水平中_竖直中对齐_法兰"
			};
		}

		public List<string> GetTShapeTeeNames()
		{
			return new List<string>
			{
				"矩形T型三通_水平左_竖直顶对齐_法兰",
				"矩形T型三通_水平右_竖直顶对齐_法兰",
				"矩形T型三通_水平中_竖直顶对齐_法兰",
				"矩形T型三通_水平左_竖直底对齐_法兰",
				"矩形T型三通_水平右_竖直底对齐_法兰",
				"矩形T型三通_水平中_竖直底对齐_法兰",
				"矩形T型三通_水平左_竖直中对齐_法兰",
				"矩形T型三通_水平右_竖直中对齐_法兰",
				"矩形T型三通_水平中_竖直中对齐_法兰"
			};
		}

		public List<string> GetRoundAngleCrossNames()
		{
			return new List<string>
			{
				"矩形圆角变径四通_水平左_竖直顶对齐_法兰",
				"矩形圆角变径四通_水平右_竖直顶对齐_法兰",
				"矩形圆角变径四通_水平中_竖直顶对齐_法兰",
				"矩形圆角变径四通_水平左_竖直底对齐_法兰",
				"矩形圆角变径四通_水平右_竖直底对齐_法兰",
				"矩形圆角变径四通_水平中_竖直底对齐_法兰",
				"矩形圆角变径四通_水平左_竖直中对齐_法兰",
				"矩形圆角变径四通_水平右_竖直中对齐_法兰",
				"矩形圆角变径四通_水平中_竖直中对齐_法兰"
			};
		}

		public List<string> GetRightAngleCrossCrossNames()
		{
			return new List<string>
			{
				"矩形直角变径四通_水平左_竖直顶对齐_法兰",
				"矩形直角变径四通_水平右_竖直顶对齐_法兰",
				"矩形直角变径四通_水平中_竖直顶对齐_法兰",
				"矩形直角变径四通_水平左_竖直底对齐_法兰",
				"矩形直角变径四通_水平右_竖直底对齐_法兰",
				"矩形直角变径四通_水平中_竖直底对齐_法兰",
				"矩形直角变径四通_水平左_竖直中对齐_法兰",
				"矩形直角变径四通_水平右_竖直中对齐_法兰",
				"矩形直角变径四通_水平中_竖直中对齐_法兰"
			};
		}

		public List<string> GetIntubationCrossNames()
		{
			return new List<string>
			{
				"矩形插管变径四通_水平左_竖直顶对齐_法兰",
				"矩形插管变径四通_水平右_竖直顶对齐_法兰",
				"矩形插管变径四通_水平中_竖直顶对齐_法兰",
				"矩形插管变径四通_水平左_竖直底对齐_法兰",
				"矩形插管变径四通_水平右_竖直底对齐_法兰",
				"矩形插管变径四通_水平中_竖直底对齐_法兰",
				"矩形插管变径四通_水平左_竖直中对齐_法兰",
				"矩形插管变径四通_水平右_竖直中对齐_法兰",
				"矩形插管变径四通_水平中_竖直中对齐_法兰"
			};
		}

		public List<string> GetConnFittingNames(ConnectDuctStyle connStyle)
		{
			List<string> result = null;
			switch (connStyle)
			{
			case ConnectDuctStyle.TShapeTee:
				result = this.GetTShapeTeeNames();
				break;
			case ConnectDuctStyle.RoundAngleTee:
				result = this.GetRoundAngleTeeNames();
				break;
			case ConnectDuctStyle.RightAngleTee:
				result = this.GetRightAngleTeeNames();
				break;
			case ConnectDuctStyle.IntubationTee:
				result = this.GetIntubationTeeNames();
				break;
			case ConnectDuctStyle.YShapeTee:
				result = this.GetYShapeTeeNames();
				break;
			case ConnectDuctStyle.RoundAngleCross:
				result = this.GetRoundAngleCrossNames();
				break;
			case ConnectDuctStyle.RightAngleCross:
				result = this.GetRightAngleCrossCrossNames();
				break;
			case ConnectDuctStyle.IntubationCross:
				result = this.GetIntubationCrossNames();
				break;
			}
			return result;
		}

		public ConnectDuctStyle GetConnDuctStyle(string specFamilyName)
		{
			ConnectDuctStyle result = ConnectDuctStyle.Empty;
			if (this.BeContainsFamilyName(ConnectDuctStyle.TShapeTee, specFamilyName))
			{
				result = ConnectDuctStyle.TShapeTee;
			}
			else if (this.BeContainsFamilyName(ConnectDuctStyle.RoundAngleTee, specFamilyName))
			{
				result = ConnectDuctStyle.RoundAngleTee;
			}
			else if (this.BeContainsFamilyName(ConnectDuctStyle.RightAngleTee, specFamilyName))
			{
				result = ConnectDuctStyle.RightAngleTee;
			}
			else if (this.BeContainsFamilyName(ConnectDuctStyle.IntubationTee, specFamilyName))
			{
				result = ConnectDuctStyle.IntubationTee;
			}
			else if (this.BeContainsFamilyName(ConnectDuctStyle.YShapeTee, specFamilyName))
			{
				result = ConnectDuctStyle.YShapeTee;
			}
			else if (this.BeContainsFamilyName(ConnectDuctStyle.RoundAngleCross, specFamilyName))
			{
				result = ConnectDuctStyle.RoundAngleCross;
			}
			else if (this.BeContainsFamilyName(ConnectDuctStyle.RightAngleCross, specFamilyName))
			{
				result = ConnectDuctStyle.RightAngleCross;
			}
			else if (this.BeContainsFamilyName(ConnectDuctStyle.IntubationCross, specFamilyName))
			{
				result = ConnectDuctStyle.IntubationCross;
			}
			return result;
		}

		private bool BeContainsFamilyName(ConnectDuctStyle connStyle, string specFamilyName)
		{
			List<string> connFittingNames = this.GetConnFittingNames(connStyle);
			return this.BeContainsFamilyName(connFittingNames, specFamilyName);
		}

		private bool BeContainsFamilyName(List<string> listFittingNames, string specFamilyName)
		{
			bool result = false;
			using (List<string>.Enumerator enumerator = listFittingNames.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Equals(specFamilyName))
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		public string GetImageName(ConnectDuctStyle connStyle, bool pointSelect)
		{
			string str = "";
			switch (connStyle)
			{
			case ConnectDuctStyle.TShapeTee:
				str = (pointSelect ? "T型三通_点选" : "T型三通");
				break;
			case ConnectDuctStyle.RoundAngleTee:
				str = (pointSelect ? "圆角三通_点选" : "圆角三通");
				break;
			case ConnectDuctStyle.RightAngleTee:
				str = (pointSelect ? "直角三通_点选" : "直角三通");
				break;
			case ConnectDuctStyle.IntubationTee:
				str = (pointSelect ? "插管三通_点选" : "插管三通");
				break;
			case ConnectDuctStyle.YShapeTee:
				str = (pointSelect ? "Y型三通_点选" : "Y型三通");
				break;
			case ConnectDuctStyle.CommonTee:
				str = (pointSelect ? "普通三通_点选" : "普通三通");
				break;
			case ConnectDuctStyle.DiversionElbow:
				str = (pointSelect ? "导流弯头_点选" : "导流弯头");
				break;
			case ConnectDuctStyle.SilencingElbow:
				str = (pointSelect ? "消声弯头_点选" : "消声弯头");
				break;
			case ConnectDuctStyle.ArcElbow:
				str = (pointSelect ? "弧形弯头_点选" : "弧形弯头");
				break;
			case ConnectDuctStyle.RoundAngleCross:
				str = (pointSelect ? "圆角四通_点选" : "圆角四通");
				break;
			case ConnectDuctStyle.RightAngleCross:
				str = (pointSelect ? "直角四通_点选" : "直角四通");
				break;
			case ConnectDuctStyle.IntubationCross:
				str = (pointSelect ? "插管四通_点选" : "插管四通");
				break;
			case ConnectDuctStyle.Side:
				str = (pointSelect ? "侧连接_点选" : "侧连接");
				break;
			case ConnectDuctStyle.Variable:
				str = (pointSelect ? "变径_点选" : "变径");
				break;
			case ConnectDuctStyle.BackForth:
				str = (pointSelect ? "来回弯_点选" : "来回弯");
				break;
			case ConnectDuctStyle.CircleSquare:
				str = (pointSelect ? "天圆地方_点选" : "天圆地方");
				break;
			case ConnectDuctStyle.RightAngleTeeRound:
				str = (pointSelect ? "圆直三通_圆形_点选" : "圆直三通_圆形");
				break;
			case ConnectDuctStyle.SlantingTeeRound:
				str = (pointSelect ? "斜三通_圆形_点选" : "斜三通_圆形");
				break;
			case ConnectDuctStyle.CommonTeeRound:
				str = (pointSelect ? "普通三通_圆形_点选" : "普通三通_圆形");
				break;
			case ConnectDuctStyle.ArcElbowRound:
				str = (pointSelect ? "内外弧形圆形弯头_圆形_点选" : "内外弧形圆形弯头_圆形");
				break;
			case ConnectDuctStyle.ShrimpElbowRound:
				str = (pointSelect ? "虾米节弯头_圆形_点选" : "虾米节弯头_圆形");
				break;
			case ConnectDuctStyle.RoundAngleCorssRound:
				str = (pointSelect ? "圆直角四通_圆形_点选" : "圆直角四通_圆形");
				break;
			case ConnectDuctStyle.IntubationCrossRound:
				str = (pointSelect ? "圆插管四通_圆形_点选" : "圆插管四通_圆形");
				break;
			case ConnectDuctStyle.SlantingCrossRound:
				str = (pointSelect ? "斜四通_圆形_点选" : "斜四通_圆形");
				break;
			case ConnectDuctStyle.SideRound:
				str = (pointSelect ? "侧连接_点选" : "侧连接");
				break;
			case ConnectDuctStyle.VariableRound:
				str = (pointSelect ? "变径_点选" : "变径");
				break;
			case ConnectDuctStyle.BackForthRound:
				str = (pointSelect ? "来回弯_点选" : "来回弯");
				break;
			}
			return Path.Combine(Product.FamilyImageLocation, "Fitting", str + ".png");
		}

		public ConnectDuctStyle GetDuctConnStyle(string connStr)
		{
			return (ConnectDuctStyle)Enum.Parse(typeof(ConnectDuctStyle), connStr, true);
		}

		public FamilySymbol GetFamilySymbol(string fsName, Autodesk.Revit.DB.Document doc, ref string loadMSG)
		{
			FamilySymbol familySymbol = null;
			if (string.IsNullOrEmpty(fsName))
			{
				loadMSG = "族名不能为空！";
				return familySymbol;
			}
			familySymbol = YJKRevitTools.GetFamilySymbol(fsName, doc);
			if (familySymbol == null)
			{
				string text = Path.Combine(Product.FamilyLocation, RevitVersion.Version, "Fitting", fsName + ".rfa");
				if (!File.Exists(text))
				{
					loadMSG = "族不存在或者加载地址不正确！";
					return familySymbol;
				}
				SubTransaction subTransaction = new SubTransaction(doc);
				subTransaction.Start();
				Family family = null;
				doc.LoadFamily(text, out family);
				if (family == null)
				{
					subTransaction.RollBack();
					return null;
				}
				familySymbol = YJKRevitTools.GetFamilySymbol(fsName, doc);
				subTransaction.Commit();
			}
			return familySymbol;
		}

		public string SetDuctReduceFitting(IList<Duct> ducts)
		{
			string result = "";
			Autodesk.Revit.DB.Document document = ducts[0].Document;
			int ductShape = this.GetDuctShape(ducts[0]);
			List<string> list = new List<string>();
			if (ductShape == 1)
			{
				list = this.GetFittingNames(ConnectDuctStyle.Variable);
			}
			if (ductShape == 0)
			{
				list = this.GetFittingNames(ConnectDuctStyle.VariableRound);
			}
			if (list.Count != 1)
			{
				return "没有找到变径族。";
			}
			FamilySymbol familySymbol = this.GetFamilySymbol(list[0], document, ref result);
			if (familySymbol == null)
			{
				return result;
			}
			foreach (Duct piping in ducts)
			{
				new YJKPiping<Duct>(piping).ChangeFirstTransition(familySymbol);
			}
			return result;
		}

		public int GetDuctShape(Duct duct)
		{
			int result;
			switch ((int)(new YJKPiping<Duct>(duct).Shape))
			{
			case -1:
				result = -1;
				break;
			case 0:
				result = 0;
				break;
			case 1:
				result = 1;
				break;
			case 2:
				result = 2;
				break;
			default:
				result = -1;
				break;
			}
			return result;
		}

		public bool BeSameShapeDucts(List<Duct> listDuct)
		{
			bool result = true;
            ConnectorProfileType connectorProfileType = ConnectorProfileType.Invalid;
			if (listDuct == null || listDuct.Count < 1)
			{
				return false;
			}
			for (int i = 0; i < listDuct.Count; i++)
			{
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(listDuct[i]);
				if (i == 0)
				{
					connectorProfileType = hypiping.Shape;
				}
				else if (hypiping.Shape != connectorProfileType)
				{
					result = false;
					break;
				}
			}
			return result;
		}
	}
}
